import PAGE from "../../../utils/middleware/page";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import store from "../../../store/index";
const app = getApp();
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        loginUserInfo: {},
        apiField: {
            module: "member",
            api: "GetLoginUser",
            data: "loginUserInfo",
            search: {},
        },
        money: null
    },
    onLoad() {
        this.fetchData();
    },
    onShow() {
        if (this.data.needRefresh) {
            this.updateNeedRefresh(false);
            this.fetchData();
        }
    },
    onPullDownRefresh() {
        this.fetchData();
        wx.stopPullDownRefresh();
    },
    async fetchData() {
        this.handleFetchData({}, "apiField", true);
    },
    handleAll() {
        this.setData({
            money: this.data.loginUserInfo.Balance
        })
    },
    handleSubmit(e) {
        var formData = e.detail.value
        if (!formData.Money) {
            wx.$Fun.showToast('提现金额不能为空')
            return
        }
        if (formData.Money > this.data.loginUserInfo.Balance) {
            wx.$Fun.showToast('提现金额不能大于可提现金额')
            return
        }

        var _this = this;
        wx.showModal({
            title: "提示",
            content: "确定要提现吗",
            success: async function (showRes) {
                if (showRes.confirm) {
                    _this.doWithdraw(formData);
                }
            },
        });
    },
    async doWithdraw(formData) {
        try {
            await wx.$Api.memberAccount.Withdraw({
                data: {
                    amount: formData.Money,
                    hasLoading: true
                }
            })

            this.updateNeedRefresh(true)
            this.setData({
                money: null
            })
            
            this.fetchData()
            wx.$Fun.showToast("提现申请成功")
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    }
}));